<?php
    #-------------------------------------------------------------#
	# This file is part of the Kumomado - Virtual Window Platform #
	# for PHP. (c) 2009 Tsai, Min Che <tirme0812@gmail.com>       #
	# Please visit http://www.kumomado.org/ for more information. #
	#                                                             #
	# This software is licensed under the GNU License v3. Please  #
	# view the LICENSE file for the full text of this license.    #
    #-------------------------------------------------------------#

	Kumomado::load('Kui.Containers.Layouts.KumomadoUILayout');
	/**
	 * @package libraries/kui/containers/layouts/form/
	 * @author Tsai, Min Che
	 * @version 1.0
	 */
	class KumomadoForm extends KumomadoUILayout {
		/**
		 * @var string
		 */
		protected $ui_name = 'KForm';
		/**
		 * @var array
		 */
		protected $kuis = array();
		/**
		 * @var string
		 */
		protected $action = '';
		/**
		 * @var string "post" or "get"
		 */
		protected $method = 'post';
		/**
		 * @var string "multipart/form-data" or "application/x-www-form-urlencoded"
		 */
		protected $enctype = 'application/x-www-form-urlencoded';
		/**
		 * @var boolean
		 */
		protected $async = true;
		/**
		 * @var mixed
		 */
		protected $render = null;
		/**
		 * Construt
		 * @param string $id
		 * @param string $action
		 * @param string $method[optional]
		 * @param string $enctype[optional]
		 * @param boolean $async[optional]
		 */
		public function __construct($id, $action, $method = 'post', $enctype = 'multipart/form-data', $async = true) {
			$this->setId($id);
			$this->setAction($action);
			$this->setMethod($method);
			$this->setEnctype($enctype);
			$this->setAsync($async);
		}
		/**
		 * @param string $action
		 */
		public function setAction($action) {
			$this->action = $action;
		}
		/**
		 * @param string $method "post" or "get"
		 */
		public function setMethod($method = 'post') {
			$this->method = strtolower($method) == 'get' ? 'get' : 'post';
		}
		/**
		 * @param string $enctype "multipart/form-data" or "application/x-www-form-urlencoded"
		 */
		public function setEnctype($enctype) {
			$this->enctype = $enctype == 'multipart/form-data' ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
		}
		/**
		 * @param boolean $async
		 */
		public function setAsync($async = true) {
			$this->async = !(false === $async);
		}
		/**
		 * Get attributes
		 */
		public function getAttrs() {
			return array(
				'id' => $this->getId(),
				'action' => $this->action,
				'method' => $this->method,
				'enctype' => $this->enctype,
				'async' => $this->async
			);
		}
		/**
		 * @param object $kui @link KumomadoUserInterface
		 * @param 
		 */
		public function add(KumomadoUserInterface $kui, $label = '') {
			if (is_subclass_of($kui, 'KumomadoUIBase')) {
				$this->kuis[$kui->getId()] = array(
					'label' => $label,
					'kui' => $kui
				);
			}
		}
		public function view() {
			$ui = array();
			$ui = $this->getAttrs();
			foreach ($this->kuis as $key => $kui) {
				$render = $kui['kui']->render();
				$ui['Kuis'][] = array(
					'Label' => $kui['label'],
					'Id' => $key,
					'View' => $render['view']
				);
			}
			$xslt = new KumomadoXsltProcessor();
			if (is_array($ui)) {
				$xslt->importArray($ui);
			}
			//echo $xslt->xml;
			$xslt->importStyleSheet(dirname(__FILE__).DIRECTORY_SEPARATOR.'KumomadoForm.xsl');
			return $xslt->transformToDoc();
		}
		public function render() {
			if ($this->render == null) {
				$this->render = array(
					'ui_name' => $this->ui_name,
					'view' => $this->view(),
					'attrs' => $this->getAttrs()
				);
			}
			$kuiClient = KumomadoUIClient::getInstance();
			$kuiClient->register($this->getId(), $this->render);
			return $this->render;
		}
	}
?>